public class MyQueue2 {
    /**
     * 用数组实现循环队列
     * 是下标循环
     */

    public int[] elem;
    public int rear;//定位队尾位置（元素添加的下标）
    public int front;

    public MyQueue2(int size) {
        elem = new int[size];
    }

    public boolean offer(int val) {
        if(isEmpty()) {
            System.out.println("队列已满");
            return false;
        }
        elem[rear] = val;
        rear = (rear+1)%elem.length;
        return true;
    }

    public int pool() {
        if(isEmpty()) {
            System.out.print("队列为空：");
            return -1;
        }
        int tmp = elem[front];
        front = (front+1)%elem.length;
        return tmp;
    }

    /**
     * 查看队头元素
     * @return
     */
    public int peek() {
        if(isEmpty()) {
            System.out.print("队列为空,无法查看");
            return -1;
        }
        return elem[front];
    }

    /**
     * 查看队尾元素
     * @return
     */
    public int peekRear() {
        if(isEmpty()) {
            System.out.print("队列为空,无法查看");
            return -1;
        }
        if(rear == 0) {
            return elem[elem.length-1];
        }else {
            return elem[rear-1];
        }
    }

    public boolean isEmpty() {
        return (rear+1)%elem.length == front;
    }
}
